//
//  ItemSettingViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 27..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ItemSettingViewController.h"

@implementation ItemSettingViewController
@synthesize itemGroupId = _itemGroupId;
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_itemGroupId release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    self.title = @"서브 관리";        
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    self.itemGroupId = nil;
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"viewWillAppear:%d", [_itemGroupId intValue]);
    [super viewWillAppear:animated];
    [self loadItemByItemGroupId:_itemGroupId];
    [self.tableView reloadData];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return YES;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
}
#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAction{
    ItemFormViewController* viewController = [[[ItemFormViewController alloc] initWithNibName:@"ItemFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController createMode:_itemGroupId];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)loadItemByItemGroupId:(NSNumber*)itemGroupId{
    self.itemGroupId = itemGroupId;
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllItemByItemGroupById:itemGroupId];
    [self.tableView reloadData];
}
#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ItemFormViewController* viewController = [[[ItemFormViewController alloc] initWithNibName:@"ItemFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController loadItem:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"item_id"]];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [[SQLHandler sharedInstance]deleteItemById:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"item_id"]];
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllItemByItemGroupById:_itemGroupId];
    [self.tableView reloadData];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    [self moveRowAtIndexPath:fromIndexPath 
                 toIndexPath:toIndexPath 
                   tableName:@"item"];
}

@end
